package stu.aistar.controller;

import javafx.scene.chart.PieChart;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import stu.aistar.dao.ICinameDao;
import stu.aistar.dao.IMovieDao;
import stu.aistar.dao.ISeatsDao;
import stu.aistar.dao.ISessionDao;
import stu.aistar.mapper.CinameMapper;
import stu.aistar.model.pojo.*;

import stu.aistar.model.vo.SeatVo;
import stu.aistar.service.ICinameService;
import stu.aistar.service.IHallService;
import stu.aistar.service.ISeatsService;
import stu.aistar.service.ISessionService;


import javax.persistence.criteria.CriteriaBuilder;
import javax.servlet.http.HttpServletRequest;
import java.util.*;


@Controller
@RequestMapping("/seat")
public class SeatController {

    private SeatVo seatVos;

    private List<Seats> seatsList;
    private Integer movieId;
    private Integer sessionId;
    private Integer hallId;
    private Integer ciNameId;
    private Map<Integer,Integer> seatMap=new HashMap<>();

    private Movie movie=null;
    @Autowired
    private IMovieDao movieDao;
    @Autowired
    private ICinameDao cinameDao;
    @Autowired
    private ICinameService cinameService;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private ISeatsService seatsService;
    @Autowired
    private ISeatsDao seatsDao;
    @Autowired
    private ISessionDao sessionDao;

    @Autowired
    private IHallService hallService;

    @Autowired
    private ISessionService sessionService;
    @GetMapping("/moviedetails")
    public String movieDetails(Model model){

        //return "/WEB-INF/404.jsp";
        String location="吴中区苏州园区启月街198号独墅湖邻里中心3层中宁国际影城(园区店)";
        model.addAttribute("location",location);
        return "map";
    }


//    @GetMapping("/movie")
//    @ResponseBody
//    public String  seatMovie(Integer movieId){
//        movie=movieDao.findMovieById(movieId);
//        return "success";
//    }

    @GetMapping("/seatsuccess")
    @ResponseBody
    public Result seat(Model model, SeatVo seatVo){
        Date date=new Date();
        seatVo.setDate(String.valueOf(date));
        seatVos = seatVo;
        return new Result("200","选座成功",seatVos);
    }

    @GetMapping("/play")
    public String play(Model model){
        System.out.println(seatVos.getSeatArea());
        if(seatVos.getSeatArea().equals("包场")){
            seatsService.updateStateAll(movieId,hallId,Integer.valueOf(seatVos.getSession()));
        }else{
            String arr[]=seatVos.getSeatArea().split(",");
            for (String s : arr) {
                System.out.println(s);
            }

            for(int i=0;i< arr.length;i++){
                Integer row=arr[i].charAt(0)-'0';
                System.out.println("hang:"+row);
                Integer col=arr[i].charAt(2)-'0';
                System.out.println("lie:"+col);
                seatMap.put(row,col);
                for (Seats seats : seatsList) {
                    if(row.equals(seats.getRow()) && col.equals(seats.getCol())){
                        seatsService.updateStateSelected(seats.getId());
                    }
                }
            }

        }
        model.addAttribute("seat",seatVos);
        model.addAttribute("user_role",0);
        return "movie-checkout";
    }

    @GetMapping("/plan")
    public String plan(Model model, Session session){
//      window.location = "/boot/seat/plans?session=ciname="+cinameIds+"&movieId"+movieIds+"&hall=1";
//      movieId=Integer.valueOf(movieIds);
//      movie=movieDao.findMovieById(movieId);
//      ciNameId=Integer.valueOf(cinameId);
//      hallId=Integer.valueOf(hall);
//      List<Session> sessionList=sessionDao.findSessionByMovieIdAndCinameIdAndHallId(movieId,ciNameId,hallId);
        Integer hid = session.getHallId();
        hallId = null;
        List<Hall> halls = hallService.findByCid(session.getCinameId());
        if (hid==null&& halls != null && halls.size()!=0){
            hallId = halls.get(0).getId();
        }else if(hid!=null){
            hallId = hid;
        }
        session.setHallId(hallId);
        List<Session> sessionList = sessionService.findByCondition(session);
        Integer sid = session.getId();
        sessionId = null;
        if (sid == null && sessionList != null && sessionList.size()!=0){
            sessionId = sessionList.get(0).getId();
        }else if(sid!=null){
            sessionId = sid;
        }

        seatsList = null;
        for (Session session1 : sessionList) {
            if (session1.getId().equals(sessionId)){
                seatsList = seatsDao.findSeatsBySessionId(Integer.valueOf(sessionId));
            }
        }
        if(sessionList.size() == 0){
            model.addAttribute("status",-1);
        }
        movieId = session.getMovieId();
        ciNameId = session.getCinameId();
        Ciname ciname1= cinameDao.findCinameById(session.getCinameId());
        model.addAttribute("seatsinit",seatsList);
        model.addAttribute("sid", sessionId);
        model.addAttribute("hall",halls);
        model.addAttribute("hallId",hallId);
        model.addAttribute("sessionList",sessionList);
        model.addAttribute("movie",movieDao.findMovieById(session.getMovieId()));
        model.addAttribute("cinameId",session.getCinameId());
        model.addAttribute("ciname", ciname1.getName());
//        ListOperations<String,Movie> listOperations = redisTemplate.opsForList();
//        listOperations.leftPush("movie1",movie);
//        System.out.println(123);
//        List<Movie> list=listOperations.range("movie1",0,0);
//        list.forEach(e-> System.out.println(e));
        return "movie-seat-plan";
    }

    @GetMapping("/freeSeat")
    @ResponseBody
    public Result freeSeat(){
        for(Map.Entry<Integer, Integer> entry: seatMap.entrySet()){
            for (Seats seats : seatsList) {
                if(entry.getKey().equals(seats.getRow()) && entry.getValue().equals(seats.getCol())){
                    seatsService.updateStateFree(seats.getId());
                }
            }
        }
        System.out.println(seatVos);
        System.out.println("seat"+seatVos.getSession());
        return new Result("200","放座成功",sessionId);
    }

}
